package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Orders;
import com.lmb.bean.R;
import com.lmb.dto.OrdersDto;
import com.lmb.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;


/**
 * @projectName: reggie
 * @package: com.lmb.controller
 * @className: OrdersController
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/21 19:30
 */
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @GetMapping("/userPage")
    public R ordersPage(Integer page, Integer pageSize, HttpSession session){

        Page<OrdersDto>  ordersDto=  ordersService.userPage(page,pageSize,session);

        return R.success(ordersDto);
    }

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders,HttpSession session){
        System.out.println(orders);

        ordersService.submit(orders,session);
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R pageList(Integer page, Integer pageSize,String number, String beginTime,String endTime) throws ParseException {
        System.out.println(page +"  111  "+pageSize);
        System.out.println(beginTime);


        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.like(number!=null,Orders::getNumber,number);
        ordersLambdaQueryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        ordersLambdaQueryWrapper.le(endTime!=null,Orders::getOrderTime,endTime);
        Page<Orders> ordersPages = ordersService.page(ordersPage,ordersLambdaQueryWrapper);

        return R.success(ordersPages);
    }

    @PutMapping
    public  R upOrderStatus(@RequestBody Orders orders){
        System.out.println(orders.getId());
        System.out.println(orders.getStatus());
        ordersService.lambdaUpdate().
                eq(Orders::getId, orders.getId()).
                set(Orders::getStatus, 3).update();
        return R.success("修改成功！！！");
    }
}
